<?php
/**
 * lemocms
 * ============================================================================
 * 版权所有 2018-2027 lemocms，并保留所有权利。
 * 网站地址: https://www.lemocms.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp6实现
 * ============================================================================
 * Author: yuege
 * Date: 2019/8/2
 */

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\StudentTopic;
use app\common\traits\Curd;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
use app\common\model\Grade as GradeModel;
use app\common\model\Topic;
use app\common\model\Schedule;

class Grade extends Backend
{
    use Curd;

    protected $user_info;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->modelClass = new GradeModel();
        $this->user_info = session('admin');
    }

    /**
     * 渲染页面
     * 作者：糜家智
     * 时间：2021/4/8 18:12
     */
    public function index()
    {
        if (Request::isPost()) {
            $arr = Db::name('grade')
                ->order('pid asc')
                ->select()->toArray();
            foreach ($arr as $k => $v) {
                $arr[$k]['lay_is_open'] = false;
            }
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $arr, 'is' => true, 'tip' => '操作成功'];
        }
        return view();
    }

    /**
     * 院系增加
     * 作者：糜家智
     * 时间：2021/4/8 18:14
     */
    public function add()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            if ($data['pid'] == 0) {
                $data['type'] = 1;
            } else {
                $type = GradeModel::where(['id' => $data['pid']])->value('type');
                $data['type'] = $type + 1;
            }
            if (empty($data['title'])) {
                $this->error(lang('rule name cannot null'));
            }
            if ($this->modelClass->create($data)) {
                $this->success(lang('add success'), url('index'));
            } else {
                $this->error(lang('add fail'));
            }
        } else {
            $list = Db::name('grade')
                ->select();
            $list = $this->modelClass->cateTree($list);

            $rule = '';
            if (Request::get('rule_id')) {
                $rule = Db::name('grade')
                    ->find(Request::get('rule_id'));
            }
            $view = [
                'info' => null,
                'ruleList' => $list,
                'rule' => $rule,
            ];
            View::assign($view);
            return view();
        }
    }

    /**
     * 院系修改
     * 作者：糜家智
     * 时间：2021/4/8 18:15
     */
    public function edit()
    {
        if (request()->isPost()) {
            $data = \request()->post();
            GradeModel::update($data);
            $this->success(lang('edit success'), url('index'));
        } else {
            $list = Db::name('grade')
                ->select();
            $list = $this->modelClass->cateTree($list);
            $id = Request::param('id');
            $info = $this->modelClass->find($id)->toArray();
            $rule = '';
            if (Request::get('rule_id')) {
                $rule = Db::name('grade')
                    ->find(Request::get('rule_id'));
            }
            $view = [
                'info' => $info,
                'ruleList' => $list,
                'rule' => $rule,
            ];
            View::assign($view);
            return view('add');
        }
    }

    /**
     * 题目根据班级分配页面渲染
     * 作者：糜家智
     * 时间：2021/4/12 9:57
     */
    public function allocation()
    {
        if (Request::isPost()) {
            $arr = Db::name('grade')
                ->order('pid asc')
                ->select()->toArray();
            foreach ($arr as $k => $v) {
                if ($v['type'] == 3){
                    if ($v['title'] != null){
                        $arr[$k]['title_name'] = Topic::where(['id' => $v['topic_id']])->value('title');
                    }
                }
                $arr[$k]['lay_is_open'] = false;
            }
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $arr, 'is' => true, 'tip' => '操作成功'];
        }
        return view();
    }

    /**
     * 题目根据班级分配
     * 作者：糜家智
     * 时间：2021/4/12 10:03
     */
    public function grade_allocation($id)
    {
        $list = $this->modelClass->find($id);
        if (empty($list)) $this->error('data is not exist');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $list->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功', url('index')) : $this->error('保存失败');
        }

        $topic = Topic::select();

        $view = [
            'info' => $list,
            'title' => '编辑',
            'topicList' => $topic
        ];

        return view('grade_allocation', $view);
    }


}